package com.lanou3g.hm0419;

import java.io.*;

/**
 * Created by dllo on 18/4/20.
 */
public class DelThread extends Thread{
    private File file;  //源文件的名字

    public DelThread(File file) {
        this.file = file;
    }

    @Override
    public void run() {

       // String path1 = file.getParent();  //查找当前文件所在路径
        File pathDir = file.getParentFile(); //直接获取当前文件所在文件夹

        String newName = file.getName().replace(".txt","-new.txt");
        // 文件夹是不能写流的
        File newFile = new File(pathDir,newName);// 所在文件夹：文件名

        BufferedReader br = null;
        PrintWriter pw = null;

        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(newFile)));

            String line = "";
            while ((line = br.readLine()) != null){
                if (line.startsWith("//")){
                    continue;  //如果以／／开头，跳过此行写下一行
                }
                pw.println(line);
            }
            pw.flush();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关流
            if (pw != null){
                pw.close();
            }
            if (br != null){
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }


    }
}
